package thread.practice;/**
 * Description
 * <p>
 * </p>
 * DATE 2019/12/23.
 *
 * @author cuihao.
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: ch-general
 * @description: 线程A输出1-10，输出到5的时候启动线程B
 * @author: cuihao
 * @create: 2019-12-23 20:55
 **/
public class Test1 {

    static ExecutorService threadPool = new ThreadPoolExecutor(5, 5,
        0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
    private static final Object lock = new Object();
    static Thread threadA = new Thread() {
        @Override
        public void run() {
            synchronized (lock) {
                int i = 1;
                while (i <= 10) {
                    if (i == 5) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程a输出 i = " + i);
                    i++;
                }
            }
        }
    };
    static Thread threadB = new Thread() {
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("线程b开始执行");
                lock.notify();
            }
        }
    };

    public static void main(String[] args) {
        // 线程A优先级高先运行，先进入线程规划化器，a启动后b立即启动
        threadA.setPriority(5);
        threadA.setPriority(3);
        threadPool.execute(threadA);
        threadPool.execute(threadB);
    }
}
